package com.jinxin.platform.cdacscontrolmgr.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.annotation.UnLoginForUser;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdacscontrolmgrRegistInfo;
import com.jinxin.platform.cdacscontrolmgr.bean.bo.ProjectInfo;
import com.jinxin.platform.cdacscontrolmgr.bean.dto.ShareInfoDTO;
import com.jinxin.platform.cdacscontrolmgr.rpc.CdacscontrolmgrRpc;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacscontrolkmgrOwnerService;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacscontrolkmgrRegistInfoService;
import com.jinxin.platform.cdacscontrolmgr.service.WXShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-26
 * @Author: Lingnan
 **/
@APCOSApi(value = "门禁业主小程序接口", developers = {"lingnan"}, description = "门禁业主小程序接口")
@Api(tags = "门禁业主小程序接口")
@RestController
@RequestMapping("/cdacscontrolmgr/cdacsmgrMiniProgram")
@Slf4j
public class CdacscontrolmgrMiniProgramController {
    @Autowired
    private CdacscontrolmgrRpc cdyardmgrRpc;
    @Autowired
    private ICdacscontrolkmgrOwnerService ownerService;
    @Autowired
    private ICdacscontrolkmgrRegistInfoService registInfoService;
    @Autowired
    private WXShareService wxShareService;

    @UnLoginForUser
    @ApiOperation(value = "来访登记")
    @APCOSApiOperation(value = "来访登记", developers = {"lingnan"}, description = "来访登记",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveVisitorRegist")
    public JsonResult saveVisitorRegist(@RequestBody CdacscontrolmgrRegistInfo record){
        return cdyardmgrRpc.saveVisitorRegist(record);
    }

    @ApiOperation(value = "生成临时密码")
    @APCOSApiOperation(value = "生成临时密码", developers = {"lingnan"}, description = "生成临时密码",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/generateTempPwd")
    public JsonResult generateTempPwd(@RequestBody CdacscontrolmgrRegistInfo record){
        return registInfoService.generateTempPwd(record);
    }

    @UnLoginForUser
    @APCOSApiOperation(value = "根据业主手机号查询项目列表", developers = {"lingnan"}, description = "根据业主手机号查询项目列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @ApiOperation(value = "根据业主手机号查询项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="业主手机号",required=true,paramType="query",dataType="String"),
    })
    @GetMapping("/queryProjectByPhone")
    public JsonResult queryProjectByPhone(String phone){
        List<ProjectInfo> projectInfos = ownerService.queryProjectByPhone(phone);
        return JsonResult.ok(projectInfos);
    }

    @APCOSApiOperation(value = "访客记录", developers = {"lingnan"}, description = "访客记录",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @ApiOperation(value = "访客记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectId",value="项目ID", paramType="query",dataType="String"),
            @ApiImplicitParam(name="visitDate",value="到访日期", paramType="query",dataType="String"),
            @ApiImplicitParam(name="ownerPhone",value="业主手机号",required = true,paramType="query",dataType="String")
    })
    @GetMapping("/queryVisitRecord")
    public JsonResult queryVisitRecord(String projectId,String visitDate,@RequestParam(name = "ownerPhone") String ownerPhone){
        if(StringUtils.isEmpty(ownerPhone)){return null;}
        HashMap param = new HashMap();
        param.put("projectId",projectId);
        param.put("visitDate",visitDate);
        param.put("ownerPhone",ownerPhone);
        List<CdacscontrolmgrRegistInfo> list = registInfoService.list(param);
        return JsonResult.ok(list);
    }

    @APCOSApiOperation(value = "删除访客记录", developers = {"lingnan"}, description = "删除访客记录",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @ApiOperation(value = "删除访客记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="记录ID", paramType="query",dataType="String")
    })
    @GetMapping("/delVisitRecord")
    public JsonResult delVisitRecord(String id){
        return registInfoService.delete(id);
    }

    @PostMapping("getShareInfo")
    @APCOSApiOperation(value = "获取分享配置信息-H5", developers = {"lingnan"}, description = "获取分享配置信息-H5",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    public JsonResult getShareInfo(@RequestBody ShareInfoDTO shareInfoDTO){
        log.info("getShareInfo  shareInfoDTO: {}", JSONObject.toJSONString(shareInfoDTO));
        return JsonResult.ok(wxShareService.getWxConfig(shareInfoDTO));
    }
}
